package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Tourist;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.TouristMapper;
import com.ruoyi.system.service.ITouristService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 旅客Service业务层处理
 *
 * @author 强盛酒店
 * @date 2023-03-07
 */
@Service
public class TouristServiceImpl implements ITouristService {
    @Autowired
    private TouristMapper touristMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询旅客
     *
     * @param id 旅客主键
     * @return 旅客
     */
    @Override
    public Tourist selectTouristById(Long id) {
        return touristMapper.selectTouristById(id);
    }

    /**
     * 查询旅客列表
     *
     * @param tourist 旅客
     * @return 旅客
     */
    @Override
    public List<Tourist> selectTouristList(Tourist tourist) {
        return touristMapper.selectTouristList(tourist);
    }

    /**
     * 新增旅客
     *
     * @param tourist 旅客
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTourist(Tourist tourist) {
        tourist.setCreateTime(DateUtils.getNowDate());
        // tourist.setCreateUser(SecurityUtils.getUserId());
        tourist.setPassword(SecurityUtils.encryptPassword(tourist.getPassword()));
        int flag = touristMapper.insertTourist(tourist);
        SysUser sysUser = new SysUser();
        sysUser.setUserName(tourist.getPhone());
        sysUser.setNickName(tourist.getName());
        sysUser.setEmail(tourist.getEmail());
        sysUser.setPhonenumber(tourist.getPhone());
        sysUser.setSex(tourist.getSex());
        sysUser.setPassword(tourist.getPassword());
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUser.setRemark(tourist.getId().toString());
        sysUserMapper.insertUser(sysUser);
        return flag;
    }

    /**
     * 修改旅客
     *
     * @param tourist 旅客
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTourist(Tourist tourist) {
        Tourist old = touristMapper.selectTouristById(tourist.getId());
        tourist.setUpdateTime(DateUtils.getNowDate());
        int flag = touristMapper.updateTourist(tourist);
        SysUser oldSysUser = sysUserMapper.selectUserByUserName2(old.getPhone());
        oldSysUser.setUserName(tourist.getPhone());
        oldSysUser.setPhonenumber(tourist.getPhone());
        oldSysUser.setEmail(tourist.getEmail());
        sysUserMapper.updateUser(oldSysUser);
        return flag;
    }

    /**
     * 批量删除旅客
     *
     * @param ids 需要删除的旅客主键
     * @return 结果
     */
    @Override
    public int deleteTouristByIds(Long[] ids) {
        for (Long id : ids) {
            Tourist old = touristMapper.selectTouristList(new Tourist() {{
                setId(id);}}).get(0);
            SysUser sysUser = sysUserMapper.selectUserByUserName2(old.getPhone());
            sysUserMapper.delete2(sysUser.getUserId());
        }
        return touristMapper.deleteTouristByIds(ids);
    }

    /**
     * 删除旅客信息
     *
     * @param id 旅客主键
     * @return 结果
     */
    @Override
    public int deleteTouristById(Long id) {
        return touristMapper.deleteTouristById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int rest(Tourist tourist) {
        if (StringUtils.isNotEmpty(tourist.getNewPassWord())){
            Tourist old = touristMapper.selectTouristById(tourist.getId());
            LoginUser loginUser = SecurityUtils.getLoginUser();
            String password = loginUser.getPassword();
            if (!SecurityUtils.matchesPassword(tourist.getPassword(), password))
            {
                Assert.isTrue(false,"修改密码失败，旧密码错误");
            }
            if (SecurityUtils.matchesPassword(tourist.getNewPassWord(), password))
            {
                Assert.isTrue(false,"新密码不能与旧密码相同");
            }
            tourist.setPassword(SecurityUtils.encryptPassword(tourist.getNewPassWord()));
        }else {
            tourist.setPassword(SecurityUtils.encryptPassword("123456"));
        }
        Tourist old = touristMapper.selectTouristList(new Tourist() {{
            setIdCard(tourist.getIdCard());
        }}).get(0);
        tourist.setUpdateTime(DateUtils.getNowDate());
        int flag = touristMapper.updateTourist(tourist);
        SysUser oldSysUser = sysUserMapper.selectUserByUserName2(old.getPhone());
        oldSysUser.setPassword(tourist.getPassword());
        sysUserMapper.updateUser(oldSysUser);
        return flag;
    }

    @Override
    public int updateTourist2(Tourist tourist) {
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        tourist.setUpdateTime(DateUtils.getNowDate());
        int flag = touristMapper.updateTourist(tourist);
        sysUser.setUserName(tourist.getPhone());
        sysUser.setNickName(tourist.getName());
        sysUser.setEmail(tourist.getEmail());
        sysUser.setPhonenumber(tourist.getPhone());
        sysUser.setSex(tourist.getSex());
        sysUserMapper.updateUser(sysUser);
        return flag;
    }

    @Override
    public Tourist selectTouristById2() {
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        return touristMapper.selectTouristById(Long.parseLong(sysUser.getRemark()));
    }
}
